package com.sky.zero.common.generate.core;

public class GenerateController extends GenerateBase {

    private String serviceName;

    public GenerateController(GenerateBase generateBase) {
        super(generateBase.packagePath, generateBase.tableName, generateBase.moduleName, generateBase.className,
                generateBase.fieldList, generateBase.fieldMethod);
        serviceName = generateBase.className + "Service" ;
    }

    public void execute() {
        StringBuilder builder = new StringBuilder();
        String generatePackage = generatePackage(packagePath + "." + moduleName + ".controller");
        String generateClass = generateClass(className);
        String generateAttribute = generateAttribute(serviceName);
        String generateMethod = generateMethod(serviceName);

        builder.append(generatePackage);
        builder.append(generateImport(generateImport(generateClass + generateAttribute + generateMethod), packagePath,
                moduleName, className));
        builder.append(generateClass);
        builder.append(generateAttribute);
        builder.append(generateMethod);

        writeGenerateFile(builder.append("}").toString().getBytes(), getGeneratePath("controller", "Controller"));

    }

    /**
     * 生成引用路径
     **/
    public String generateImport(String importJavaPath, String packagePath, String moduleName, String className) {
        StringBuilder builder = new StringBuilder();
        builder.append(importJavaPath);
        String importPath = packagePath + "." + moduleName;
        builder.append("import ").append(importPath).append(".entity.").append(className).append(";\n");
        builder.append("import ").append(importPath).append(".entity.").append(className).append("Param;\n");
        builder.append("import ").append(importPath).append(".entity.").append(className).append("VO;\n");
        builder.append("import ").append(importPath).append(".service.").append(serviceName).append(";\n");
        builder.append("\n\n");
        return builder.toString();
    }

    /**
     * 生成类
     **/
    public String generateClass(String className) {
        StringBuilder builder = new StringBuilder();
        builder.append("@RestController\n");
        builder.append("@RequestMapping(\"/").append(className.toLowerCase()).append("\")\n");
        builder.append("public class ").append(className)
                .append("Controller implements " + className + "Api {\n\n");
        return builder.toString();
    }

    /**
     * 生成属性
     **/
    public String generateAttribute(String serviceName) {
        StringBuilder builder = new StringBuilder();
        builder.append("	@Autowired\n");
        builder.append("	private ").append(serviceName).append(" ").append(lower(serviceName)).append(";\n\n");
        return builder.toString();
    }

    /**
     * 生成方法
     **/
    public String generateMethod(String serviceName) {
        StringBuilder b = new StringBuilder();
        // 查询列表(分页)
        b.append("	/** 查询列表(分页) **/\n");
        b.append("	@Override\n");
        b.append("    public PageInfo<").append(className).append("VO> get").append(className).append("Page(")
                .append(className).append("Param ").append("param){\n");
        b.append("		return ").append(lower(serviceName)).append(".get").append(className).append("Page(param);\n	}\n\n");

        // 查询列表
        b.append("	/** 查询列表 **/\n");
        b.append("	@Override\n");
        b.append("    public List<").append(className).append("VO> get").append(className).append("List(").append(className).append("Param param) {\n	");
        b.append("	return ").append(lower(serviceName)).append(".get").append(className).append("List(param);\n	}\n\n");

        // 根据ID获取数据
//        b.append("    /** 根据ID获取详情**/\n");
//        b.append("	@Override\n");
//        b.append("    public ").append(className).append("VO get").append(className).append("ById(").append(fieldInfo.getJavaDataType()).append(" id) {\n	");
//        b.append("	return ").append(lower(serviceName)).append(".get").append(className).append("ById(id);\n	}\n\n");

        // 新增
        b.append("    /**新增**/\n");
        b.append("	@Override\n");
        b.append("    public Integer add").append(className).append("(").append(className).append(" param) {\n ");
        b.append("	    return ").append(lower(serviceName)).append(".insert").append(className).append("(param);\n     }\n\n");

        // 批量新增
        b.append("    /**批量新增**/\n");
        b.append("	@Override\n");
        b.append("    public Integer addBatch").append(className).append("(List<").append(className).append("> paramList) {\n ");
        b.append("	    return ").append(lower(serviceName)).append(".insertBatch").append(className).append("(paramList);\n     }\n\n");

        // 编辑
        b.append("    /**编辑**/\n");
        b.append("	@Override\n");
        b.append("    public Integer edit").append(className).append("(").append(className).append(" param) {\n ");
        b.append("	    return ").append(lower(serviceName)).append(".update").append(className).append("(param);\n     }\n\n");

        // 批量编辑
        b.append("    /**批量编辑**/\n");
        b.append("	@Override\n");
        b.append("    public Integer editBatch").append(className).append("(List<").append(className).append("> paramList) {\n ");
        b.append("	    return ").append(lower(serviceName)).append(".updateBatch").append(className).append("(paramList);\n     }\n\n");

        // 根据ID删除数据
//        b.append("    /** 根据ID删除**/\n");
//        b.append("	@Override\n");
//        b.append("    public Integer del").append(className).append("ById(").append(fieldInfo.getJavaDataType()).append(" id) {\n	");
//        b.append("	return ").append(lower(serviceName)).append(".del").append(className).append("ById(id);\n	}\n\n");

        // 根据ID批量删除数据
//        b.append("    /** 根据ID批量删除**/\n");
//        b.append("	@Override\n");
//        b.append("    public Integer del").append(className).append("ByIds(List<").append(fieldInfo.getJavaDataType()).append("> ids) {\n	");
//        b.append("	return ").append(lower(serviceName)).append(".del").append(className).append("ByIds(ids);\n	}\n\n");

        return b.toString();
    }

}
